package org.MobileSales.DataManagement;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.MobileSales.Model.Clients.ClientGroup;
import org.MobileSales.Model.Params.Param;
import org.MobileSales.Model.Tasks.CyclicTask;
import org.MobileSales.Model.Tasks.Schedule;
import org.MobileSales.Model.Tasks.Task;
import org.MobileSales.Model.Tasks.TaskStatus;
import org.MobileSales.Model.Tasks.TaskType;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TasksManager {
	public Context context;
	
	public TasksManager(Context c)
	{
		this.context = c;
	}
	public Schedule GetSchedule(int scheduleId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, EmployeeId, CreationDate, ModificationDate, StartTime, EndTime, StartPosLat, StartPosLon, EndPosLat, EndPosLon, RealStartTime, RealEndTime" 
							+ " FROM Schedules" 
							+ " WHERE _id = " + scheduleId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Schedule result = new Schedule();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.EmployeeId = c.getInt(c.getColumnIndex("EmployeeId"));
     		result.CreationDate = c.getString(c.getColumnIndex("CreationDate"));
     		result.ModificationDate = c.getString(c.getColumnIndex("ModificationDate"));
     		result.StartTime = c.getString(c.getColumnIndex("StartTime"));
     		result.EndTime = c.getString(c.getColumnIndex("EndTime"));
     		result.StartPositionLatitude = c.getDouble(c.getColumnIndex("StartPosLat"));
     		result.StartPositionLongitude = c.getDouble(c.getColumnIndex("StartPosLon"));
     		result.EndPositionLatitude = c.getDouble(c.getColumnIndex("EndPosLat"));
     		result.EndPositionLongitude = c.getDouble(c.getColumnIndex("EndPosLon"));
     		result.RealStartTime = c.getString(c.getColumnIndex("RealStartTime"));
     		result.RealEndTime = c.getString(c.getColumnIndex("RealEndTime"));
     		result.Tasks = GetTaskList(result.Id);
	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public void StoreSchedule(Schedule schedule)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		String now = new Date().toString();
			ContentValues cv = new ContentValues();
			if (schedule.Id == 0){
				cv.put("CreationDate",now);
			}
			cv.put("EndPositionLatitude", schedule.EndPositionLatitude);
			cv.put("EndPositionLongitude", schedule.EndPositionLongitude);
			cv.put("EndTime", schedule.EndTime);
			cv.put("ModificationDate", now);
			cv.put("RealEndTime", schedule.RealEndTime);
			cv.put("RealStartTime", schedule.RealStartTime);
			cv.put("StartPositionLatitude", schedule.StartPositionLatitude);
			cv.put("StartPositionLongitude", schedule.StartPositionLongitude);
			cv.put("StartTime", schedule.StartTime);
			cv.put("EmployeeId", schedule.EmployeeId);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (schedule.Id != 0)
				db.update("Schedules", cv, "_id ="+schedule.Id, null);
			else
				db.insert("Schedules", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveSchedule(int scheduleId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("Schedules", "_id ="+scheduleId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void StartSchedule(int scheduleId, Date time, double latitude, double longitude)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("RealStartTime", time.toString());
			cv.put("StartPositionLatitude", latitude);
			cv.put("StartPositionLongitude", longitude);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (scheduleId != 0)
				db.update("Schedules", cv, "_id ="+scheduleId, null);

     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}

	public void EndSchedule(int scheduleId, Date time, double latitude, double longitude)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("RealEndTime", time.toString());
			cv.put("EndPositionLatitude", latitude);
			cv.put("EndPositionLongitude", longitude);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (scheduleId != 0)
				db.update("Schedules", cv, "_id ="+scheduleId, null);

     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}

	public Task GetTask(int taskId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, CreatorId, TaskStatusId, TaskTypeId, CreationDate, ModificationDate, StartTime, EndTime, StartPosLat, StartPosLon, EndPosLat, EndPosLon, RealStartTime, RealEndTime, ClientId, Info, Priority, ScheduleId" 
							+ " FROM Tasks" 
							+ " WHERE _id = " + taskId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Task result = new Task();
     		FillTaskFromCursor(c, result);		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public Iterable<Task> GetTaskList(int scheduleId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
     		String query = "SELECT _id" 
				+ " FROM Tasks" 
				+ " WHERE _id = " + scheduleId;
     		Cursor c = db.rawQuery(query, null);

	 		List<Task> result = new ArrayList<Task>();
    		if (c.moveToFirst()){
	     		while (c!=null){    	     		
	         		result.add(GetTask(c.getInt(c.getColumnIndex("_id"))));
	         		c.moveToNext();
	     		}
	     		return result;
	     	}
    		
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
     	return null;
	}
	
	public CyclicTask GetCyclicTask(int taskId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, CreatorId, TaskStatusId, TaskTypeId, CreationDate, ModificationDate, StartTime, EndTime, StartPosLat, StartPosLon, EndPosLat, EndPosLon, RealStartTime, RealEndTime, ClientId, Info, Priority, ScheduleId" 
					+ " FROM Tasks" 
					+ " WHERE _id = " + taskId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		CyclicTask result = new CyclicTask();
     		FillTaskFromCursor(c, result);
     		
     		query = "SELECT EmployeeId, TaskInterval"
     			+ " FROM EmployeeHasCyclicTasks"
     			+ " WHERE TaskId = " + taskId;
     		c = db.rawQuery(query, null);
     		if (!c.moveToFirst())
     			return null;
     		
     		result.EmployeeId = c.getInt(c.getColumnIndex("EmployeeId"));
     		result.TaskInterval = c.getInt(c.getColumnIndex("TaskInterval"));
      		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public Iterable<CyclicTask> GetCyclicTaskList(int employeeId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT TaskId" 
					+ " FROM EmployeeHasCyclicTasks" 
					+ " WHERE _id = " + employeeId;
     		Cursor c = db.rawQuery(query, null);
     	
     		if (!c.moveToFirst())
     			return null;
     		List<CyclicTask> result = new ArrayList<CyclicTask>();
	     	while (c!=null){    	     		
	         	result.add(GetCyclicTask(c.getInt(c.getColumnIndex("TaskId"))));
	         	c.moveToNext();
	     	}
	     	
	     	return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public void FillTaskFromCursor(Cursor c, Task t)
	{
 		t.Id = c.getInt(c.getColumnIndex("_id"));
 		t.CreatorId = c.getInt(c.getColumnIndex("CreatorId"));
 		t.ScheduleId = c.getInt(c.getColumnIndex("ScheduleId"));
 		t.TaskType = GetTaskType(c.getInt(c.getColumnIndex("TaskTypeId")));
 		t.TaskStatus = GetTaskStatus(c.getInt(c.getColumnIndex("TaskStatusId")));
 		t.ClientId = c.getInt(c.getColumnIndex("ClientId"));
 		t.Info = c.getString(c.getColumnIndex("Info"));
 		t.Priority = c.getInt(c.getColumnIndex("Priority"));
 		t.CreationDate = c.getString(c.getColumnIndex("CreationDate"));
 		t.ModificationDate = c.getString(c.getColumnIndex("ModificationDate"));
 		t.StartTime = c.getString(c.getColumnIndex("StartTime"));
 		t.EndTime = c.getString(c.getColumnIndex("EndTime"));
 		t.StartPositionLatitude = c.getDouble(c.getColumnIndex("StartPosLat"));
 		t.StartPositionLongitude = c.getDouble(c.getColumnIndex("StartPosLon"));
 		t.EndPositionLatitude = c.getDouble(c.getColumnIndex("EndPosLat"));
 		t.EndPositionLongitude = c.getDouble(c.getColumnIndex("EndPosLon"));
 		t.RealStartTime = c.getString(c.getColumnIndex("RealStartTime"));
 		t.RealEndTime = c.getString(c.getColumnIndex("RealEndTime")); 		  
	}
	
	public void StoreTask(Task task, int employeeId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		String now = new Date().toString();
			ContentValues cv = new ContentValues();
			if (task.Id == 0){
				cv.put("CreationDate",now);
			}
			cv.put("CreatorId", task.CreatorId);
			cv.put("TaskStatusId", task.TaskStatus.Id);
			cv.put("TaskTypeId", task.TaskType.Id);
			cv.put("ClientId", task.ClientId);
			cv.put("Info", task.Info);
			cv.put("Priority", task.Priority);
			cv.put("ScheduleId", task.ScheduleId);
			cv.put("EndPositionLatitude", task.EndPositionLatitude);
			cv.put("EndPositionLongitude", task.EndPositionLongitude);
			cv.put("EndTime", task.EndTime);
			cv.put("ModificationDate", now);
			cv.put("RealEndTime", task.RealEndTime);
			cv.put("RealStartTime", task.RealStartTime);
			cv.put("StartPositionLatitude", task.StartPositionLatitude);
			cv.put("StartPositionLongitude", task.StartPositionLongitude);
			cv.put("StartTime", task.StartTime);
			
     		dbHelper.openDatabase();
			SQLiteDatabase db = dbHelper.getDatabase();
			long taskId = -1;
			if (task.Id != 0){
				db.update("Tasks", cv, "_id ="+task.Id, null);
				taskId = task.Id;
			}
			else
				taskId = db.insert("Tasks", null, cv);
     		
			Schedule schedule = null;
			
			int scheduleId = -1;
			if (task.ScheduleId != null)
			{
	     		String query = "SELECT _id" 
						+ " FROM Schedules" 
						+ " WHERE _id = " + task.ScheduleId;
	     		Cursor c = db.rawQuery(query, null);
	     		if (c.moveToFirst())
	     			schedule = GetSchedule(c.getInt(c.getColumnIndex("_id")));
	     		scheduleId = task.ScheduleId;
			}
			boolean isBeingAdded = false;
			if (schedule == null){
	     		String query = "SELECT _id" 
					+ " FROM Schedules" 
					+ " WHERE EmployeeId = " + employeeId + " and StartTime = "+task.StartTime;
	     		Cursor c = db.rawQuery(query, null);
	     		if (c.moveToFirst()){
	     			schedule = GetSchedule(c.getInt(c.getColumnIndex("_id")));
	     			scheduleId = schedule.Id;
	     		}
	     		
	     		if (schedule == null){
	     			isBeingAdded = true;
	     			cv = new ContentValues();
	     			
	     			cv.put("CreationDate", now);
	     			cv.put("StartTime", task.StartTime);
	     			cv.put("EndTime", task.EndTime);
	     			cv.put("EmployeeId", employeeId);
	     			cv.put("ModificationDate", now);
	     			
					scheduleId =(int) db.insert("Schedules", null, cv);
					cv = new ContentValues();
					cv.put("ScheduleId", (int)scheduleId);
					
					db.update("Tasks", cv, "_id = " + taskId, null);
	     		}
			}
			if (!isBeingAdded){
	     		String query = "SELECT _id" 
					+ " FROM Tasks" 
					+ " WHERE ScheduleId = " + scheduleId + " and StartTime < "+task.EndTime + " and EndTime >" + task.StartTime;
	     		Cursor c = db.rawQuery(query, null);
				boolean collide = c.getCount() != 0;

                if (collide)
                    throw new Exception("Task collides with schedule");

                // move schedule time boundries
                boolean update = false;
                if (Date.parse(schedule.StartTime) > Date.parse(task.StartTime)){ schedule.StartTime = task.StartTime; update = true; }
                if (Date.parse(schedule.EndTime) < Date.parse(task.EndTime)) { schedule.EndTime = task.EndTime;update = true; }
                if (update)
                	StoreSchedule(schedule);
			}
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void StoreCyclicTask(CyclicTask cyclicTask, int employeeId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		String now = new Date().toString();
			ContentValues cv = new ContentValues();

			if (cyclicTask.Id == 0){
				cv.put("CreationDate",now);
			}
			
			cv.put("CreatorId", cyclicTask.CreatorId);
			cv.put("TaskStatusId", cyclicTask.TaskStatus.Id);
			cv.put("TaskTypeId", cyclicTask.TaskType.Id);
			cv.put("ClientId", cyclicTask.ClientId);
			cv.put("Info", cyclicTask.Info);
			cv.put("Priority", cyclicTask.Priority);
			cv.put("ScheduleId", cyclicTask.ScheduleId);
			cv.put("EndPositionLatitude", cyclicTask.EndPositionLatitude);
			cv.put("EndPositionLongitude", cyclicTask.EndPositionLongitude);
			cv.put("EndTime", cyclicTask.EndTime);
			cv.put("ModificationDate", now);
			cv.put("RealEndTime", cyclicTask.RealEndTime);
			cv.put("RealStartTime", cyclicTask.RealStartTime);
			cv.put("StartPositionLatitude", cyclicTask.StartPositionLatitude);
			cv.put("StartPositionLongitude", cyclicTask.StartPositionLongitude);
			cv.put("StartTime", cyclicTask.StartTime);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (cyclicTask.Id != 0){
				db.update("Tasks", cv, "_id ="+cyclicTask.Id, null);
				cv = new ContentValues();
				cv.put("TaskId", cyclicTask.Id);
				cv.put("EmployeeId", cyclicTask.EmployeeId);
				cv.put("TaskInterval", cyclicTask.TaskInterval);
			}
			else{
				long taskId = db.insert("Tasks", null, cv);
				cv = new ContentValues();
				cv.put("TaskId", (int)taskId);
				cv.put("EmployeeId", cyclicTask.EmployeeId);
				cv.put("TaskInterval", cyclicTask.TaskInterval);
				db.insert("EmployeeHasCyclicTasks", null, cv);
			}
			

     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveTask(int taskId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("Tasks", "_id ="+taskId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void StartTask(int taskId, Date time, double latitude, double longitude)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("RealStartTime", time.toString());
			cv.put("StartPositionLatitude", latitude);
			cv.put("StartPositionLongitude", longitude);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (taskId != 0)
				db.update("Tasks", cv, "_id ="+taskId, null);

     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}

	public void EndTask(int taskId, Date time, double latitude, double longitude)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("RealEndTime", time.toString());
			cv.put("EndPositionLatitude", latitude);
			cv.put("EndPositionLongitude", longitude);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (taskId != 0)
				db.update("Tasks", cv, "_id ="+taskId, null);

     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public TaskStatus GetTaskStatus(int taskStatusid)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, Name, Description" 
							+ " FROM TaskStatus" 
							+ " WHERE _id = " + taskStatusid;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		TaskStatus result = new TaskStatus();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.Name = c.getString(c.getColumnIndex("Name"));
     		result.Description = c.getString(c.getColumnIndex("Description"));
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public void StoreTaskStatus(TaskStatus taskStatus)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("Name",taskStatus.Name);
			cv.put("Description", taskStatus.Description);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (taskStatus.Id != 0)
				db.update("TaskStatus", cv, "_id ="+taskStatus.Id, null);
			else
				db.insert("TaskStatus", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveTaskStatus(int taskStatusId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("TaskStatus", "_id ="+taskStatusId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public TaskType GetTaskType(int taskTypeId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, Name, Description" 
							+ " FROM TaskTypes" 
							+ " WHERE _id = " + taskTypeId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		TaskType result = new TaskType();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.Name = c.getString(c.getColumnIndex("Name"));
     		result.Description = c.getString(c.getColumnIndex("Description"));
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	
	public void StoreTaskType(TaskType taskType)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("Name",taskType.Name);
			cv.put("Description", taskType.Description);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (taskType.Id != 0)
				db.update("TaskTypes", cv, "_id ="+taskType.Id, null);
			else
				db.insert("TaskTypes", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveTaskTypes(int taskTypeId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("TaskTypes", "_id ="+taskTypeId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
}
